package org.dromara.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.erp.domain.ErpProducts;
import org.dromara.erp.mapper.ErpProductsMapper;
import org.dromara.erp.service.IErpProductsService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 商品管理Service业务层处理
 *
 * @author Mr Lei
 * @date 2024-06-01
 */
@RequiredArgsConstructor
@Service
public class ErpProductsServiceImpl implements IErpProductsService {

    private final ErpProductsMapper baseMapper;

    /**
     * 查询商品管理
     */
    @Override
    public ErpProducts queryById(Long id){
        return baseMapper.findById(id);
    }

    /**
     * 查询商品管理列表
     */
    @Override
    public TableDataInfo<ErpProducts> queryPageList(ErpProducts bo, PageQuery pageQuery) {
        Page<ErpProducts> result = baseMapper.queryPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品管理列表
     */
    @Override
    public List<ErpProducts> queryList(ErpProducts bo) {
        LambdaQueryWrapper<ErpProducts> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<ErpProducts> buildQueryWrapper(ErpProducts bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpProducts> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ErpProducts::getName, bo.getName());
        lqw.eq(bo.getCateId() != null, ErpProducts::getCateId, bo.getCateId());
        lqw.eq(StringUtils.isNotBlank(bo.getImgUrl()), ErpProducts::getImgUrl, bo.getImgUrl());
        lqw.eq(bo.getWarehouseId() != null, ErpProducts::getWarehouseId, bo.getWarehouseId());
        return lqw;
    }

    /**
     * 新增商品管理
     */
    @Override
    public Boolean insert(ErpProducts bo) {
        validEntityBeforeSave(bo);
        return baseMapper.insert(bo) > 0;
    }

    /**
     * 修改商品管理
     */
    @Override
    public Boolean update(ErpProducts bo) {
        validEntityBeforeSave(bo);
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpProducts entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
